<template>
    <container type="left-aside" :show="{header:formShow}" class="g-width100 g-height100" header-style="height:auto;" main-style="padding:0;">
        <el-scrollbar slot="aside" class="g-height100"
                      wrapStyle="overflow-x:auto;"
        >
        <tree :data-url="$baseURL.base +   '/base/func/tree'" v-on:nodeClickExt="treeNodeClickExt"
              :init-data-param-format="$_funcTreePop_initDataParamFormat"
              :select="$_funcTreePop_select()"
              relation-filter="select">

        </tree>
        </el-scrollbar>
        <Form slot="header" inline :layout="[3,3]" :layout-col-max="6"
              ref="form"
              submit-bus-key="funManageList"
              :form-items="formItems"
              label-width=""
        ></Form>
        <Table slot="main"
               ref="table"
               form-submit-bus-key="funManageList"
               :columns="columns"
               :operations="tableOperations"
               unique-label="name"
               v-on:toolbarSearchClick="formShow = !formShow"
        ></Table>
    </container>
</template>

<script>
    import Container from '../../../../common-util/src/components/page/Container.vue'
    import Form from '../../../../common-util/src/components/page/Form.vue'
    import Table from '../../../../common-util/src/components/page/Table.vue'
    import Tree from '../../../../common-util/src/components/common/Tree.vue'
    import FuncTreePopRelationFilterMixin from './FuncTreePopRelationFilterMixin'

    export default {
        name: "Func",
        mixins: [FuncTreePopRelationFilterMixin],

        components:{
            Form,
            Table,
            Tree,
            Container
        },
        data(){
            return {
                formShow: true,
                formItems:[
                    {
                        field: {
                            name: 'code'
                        },
                        element:{
                            label: '编码',
                        }
                    },
                    {
                        field: {
                            name: 'name'
                        },
                        element:{
                            label: '名称',
                        }
                    },
                    {
                        field: {
                            name: 'applicationId'
                        },
                        element:{
                            label: '应用',
                            type:'select',
                            select:{
                                url: this.$baseURL.base +  '/base/application/list'
                            },
                        }
                    },
                    {
                        field: {
                            name: 'typeDictId'
                        },
                        element:{
                            type: 'selectDict',
                            selectDict:{
                                groupCode: 'func_type'
                            },
                            label: '类型'
                        }
                    },
                    {
                        field: {
                            name: 'parentId',
                            parentId__label: null
                        },
                        element:{
                            type: 'inputSelectTree',
                            inputSelectTree:{
                                dataUrl:this.$baseURL.base +  '/base/func/tree',
                                treeInitDataParamFormat: this.$_funcTreePop_initDataParamFormat,
                            },
                            treePopRelationFilter: 'select',
                            treePopSelect: this.$_funcTreePop_select(),
                            label: '父级'
                        }
                    },
                    {
                        element:{
                            type: 'button',
                            button:[
                                {
                                    action: 'submit',
                                    requestMethod:'get',
                                    url: this.$baseURL.base +  '/base/func/listPage',
                                    label: '查询'
                                },
                                {
                                    action: 'reset',
                                    label: '重置'
                                }
                            ]
                        }
                    }
                ],
                columns:[
                    {
                        prop: 'code',
                        label:'编码'
                    },
                    {
                        prop: 'name',
                        label:'名称'
                    },
                    {
                        prop: 'applicationName',
                        label:'应用'
                    },
                    {
                        prop: 'parentName',
                        label:'父级'
                    },
                    {
                        prop: 'isDisabled',
                        label:'是否禁用'
                    },
                    {
                        prop: 'typeDictName',
                        label:'类型'
                    },
                    {
                        prop: 'url',
                        label:'地址',
                        showInDetail: true
                    },
                    {
                        prop: 'permissions',
                        label:'权限',
                        showInDetail: true
                    },
                    {
                        prop: 'remark',
                        label: '备注',
                        showInDetail: true
                    },
                    {
                        prop: 'seq',
                        label:'排序'
                    }
                ],
                tableOperations:[
                    {
                        action: 'nav',
                        url:'/func/funcAdd',
                        label: '添加'
                    },
                    {
                        action: 'nav',
                        disabledOnMissingSelect:true,
                        url:'/func/funcEdit/:id',
                        label: '编辑'
                    },
                    {
                        action: 'delete',
                        url:this.$baseURL.base +  '/base/func/:id',
                        disabledOnMissingSelect:true,
                        label:'删除'
                    },
                    {
                        action: 'detail',
                        disabledOnMissingSelect:true,
                        label:'详情'
                    },
                    {
                        action: this.enableOrDisable,
                        position:'more',
                        disabledOnMissingSelect:true,
                        label:(row) => {
                            if(row){
                                return row.isDisabled ? '启用': '禁用'
                            }else {
                                return '启用/禁用'
                            }
                        }
                    },
                    {
                        action: 'nav',
                        url:'/func/funcAdd',
                        routeQuery:['id','name'],
                        position:'more',
                        disabledOnMissingSelect:true,
                        label:'添加下级功能'
                    },
                    {
                        action: 'nav',
                        url:'/func/assignRole/:id',
                        position:'more',
                        disabledOnMissingSelect:true,
                        label:'分配角色'
                    }
                ]
            }
        },
        methods:{
            enableOrDisable(row){
                this.$prompt('请输入'+ row.name + (row.isDisabled ? '启用': '禁用') +'原因', '提示', {
                    inputErrorMessage: '原因不能为空',
                    inputValidator:(value)=>{return !!value}
                }).then(({ value }) => {
                    let data = {
                        disabled: !row.isDisabled,
                        version: row.version,
                        disabledReason: value
                    }
                    this.axios.patch(this.$baseURL.base +  '/base/func/'+ row.id,data)
                        .then(res => {
                            this.$message.success('操作成功')
                            // 重新加载数据
                            this.$refs.table.toolbarRefreshClick()
                        })
                        .catch(error => {
                            if(error.response){
                                if(error.response.status == 401){
                                    return
                                }
                                if(error.response.status == 404){
                                    this.$message.error('数据不存在，请刷新数据再试')
                                }else {
                                    this.$message.error(error.response.data.msg)
                                }
                            }else {
                                this.$message.error('网络错误或服务不可用')
                            }
                        })
                })
            },
            treeNodeClickExt (data) {
                this.$refs.form.setFormItem('parentId',data.key.id)
                this.$refs.form.setFormItem('parentId__label',data.key.label)
                this.$refs.form.refreshData()
            },
        }
    }
</script>

<style scoped>

</style>